package ModelLayer;
import java.util.ArrayList;

public class Addressbook
{
    // instance variables - replace the example below with your own
    private static Addressbook instance;
    
    private ArrayList<Person> addressbook;
    
    private Addressbook()
    {
        addressbook = new ArrayList<Person>();
    }
    
    //By using the singleton pattern, this returns the static instance of Addressbook
    public static Addressbook getInstance()
    {
        if(instance == null)
        {
            instance = new Addressbook();
        }
                
        return instance;
    }
    
    //Adds a person to the ArrayList addressbook
    public void addPerson(Person newPerson)
    {
        addressbook.add(newPerson);
    }
    
    //Initiates a search for an object in the ArrayList addressbook, by comparing the id. Once found, the while loop ends and calls the getInfo() method in Person
    public void getPerson(long id)
    {
        //this method is to search the container for a person
        //with an id-number equal to the parameter id
        //when person is found, the method is to call print-methods in the TUI layer
        boolean found = false;
        int index = 0;
        while(index < addressbook.size() && found == false)
        {
            String id1 = String.valueOf(id);
            String idIndex = String.valueOf(addressbook.get(index).getId());
            
            if(idIndex.equals(id1))
            {
                addressbook.get(index).getInfo();
                found = true;
            }
            else
            {
                index++;
            }
        }
        

            if(found==false)
            {
                System.out.println("You don't have a person with that ID... maybe you should check the addressbook?");
            }
    }
    
    //Similar to getPerson this method searches the list but when the object is found, it is deleted
    public void deletePerson(long id)
    {
        int index = 0;
        boolean found = false;

        while(index < addressbook.size() && found == false)
        {
            
            
            String id1 = String.valueOf(id);
            String idIndex = String.valueOf(addressbook.get(index).getId());
            
            
            if(idIndex.equals(id1))
            {
                addressbook.remove(index);
                System.out.println("Person has been deleted!");
                found = true;
            }
            else
            {
                index++;
            }
        }
        if(found==false)
            {
                System.out.println("You don't have a person with that ID to delete... maybe you should check the addressbook?");
            }
    }
    
    //Updates the attributes for Person
    public void updatePerson(long id, String name, String address, String postalCode,
    String city, String phoneNr)
    {
        int index = 0;
        boolean found = false;

        while(index < addressbook.size() && found == false)
        {
            String id1 = String.valueOf(id);
            String idIndex = String.valueOf(addressbook.get(index).getId());
                        
            if(idIndex.equals(id1))
            {
                addressbook.get(index).changeInfo(id, name, address, postalCode, city, phoneNr);
                found = true;
                System.out.println("Person has been updated!");
            }
            else
            {
                index++;
            }
        }
        if(found==false)
            {
                System.out.println("You don't have a person with that ID to update... maybe you should check your addressbook?");
            }
    }
    
    //Runs a 'for-each' loop, calling the method getInfo() on each object in the list
    public void listPersons()
    {
        for(Person person : addressbook)
        {
            System.out.println("--------");
            person.getInfo();
        }
        System.out.println("--------");
    }
}